<template>
  <!-- <addEditBaseStudentView ref="addEditBaseStudentView" /> -->
  <addFileView ref="addFileView" />
  <el-main>
    <div class="teacher">
      <div class="app-table">
        <div class="app-body">
          <div class="app-search">
            <el-form :inline="true" class="demo-form-inline" v-model="form">
              <el-form-item label="所在年级" :label-width="formLabelWidth" prop="gradeId">
                <el-select v-model="form.gradeId" autocomplete="off" @change="filterGradeId" placeholder="请选择年级" style="width:190px;">
                  <el-option v-for="gradeId in gradeIds" :key="gradeId" :value="gradeId"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="班级名称" :label-width="formLabelWidth" prop="name">
                <el-select clearable v-model="form.classId" autocomplete="off" placeholder="请选择班级" style="width:190px;">
                  <el-option v-for="item in classList" :key="item.classId" :value="item.classId"
                    :label="item.name"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="学生姓名">
                <el-input placeholder="请输入学生姓名" v-model="form.name"></el-input>
              </el-form-item>
              <!-- <el-form-item label="身份证号">
                                            <el-input
                                                    placeholder="请输入身份证号"
                                                    v-model="form.idCard"
                                            ></el-input>
                                        </el-form-item>
                                        <el-form-item label="密码">
                                            <el-input
                                                    placeholder="请输入密码"
                                                    v-model="form.password"
                                            ></el-input>
                                        </el-form-item>
                                        <el-form-item label="联系电话">
                                            <el-input
                                                    placeholder="请输入联系电话"
                                                    v-model="form.mobile"
                                            ></el-input>
                                        </el-form-item>
                                        <el-form-item label="学生类型">
                                            <el-input
                                                    placeholder="请输入学生类型"
                                                    v-model="form.typeName"
                                            ></el-input>
                                        </el-form-item> -->
              <el-form-item>
                <el-button type="primary" plain @click="onSubmit" @keyup.enter="searchEnter">搜索
                </el-button>
              </el-form-item>
            </el-form>
          </div>
          <el-table size="small" :data="list" style="width: 100%" border :row-class-name="tableRowClassName">
            <el-table-column prop="id" label="id" />
            <el-table-column prop="studentNumber" label="学生学号" />
            <el-table-column prop="name" label="学生姓名" />
            <el-table-column prop="sex" label="学生性别">
              <template #default="scope">
                {{ scope.row.sex == 1 ? "男" : "女" }}
              </template>
            </el-table-column>
            <!-- <el-table-column prop="idCard" label="身份证号"/> -->
            <!-- <el-table-column prop="gradeId" label="年级"/> -->
            <el-table-column prop="clazzName" label="班级">
              <template #default="scope">
                高{{ scope.row.gradeId }}级({{ scope.row.classId }})班
              </template>
            </el-table-column>
            <!-- <el-table-column prop="departmentId" label="院系"/> -->
            <!-- <el-table-column prop="password" label="密码"/> -->
            <!-- <el-table-column prop="mobile" label="联系电话" /> -->
            <!-- <el-table-column prop="typeName" label="学生状态"/> -->
            <!-- <el-table-column prop="isEffect" label="是否有效"/> -->
            <!-- <el-table-column prop="campus" label="学校"/> -->
            <el-table-column label="操作" width="140px">
              <template style="display: block" #default="scope">
                <el-button size="small" type="success" @click="editBaseStudent(scope.row)">评价分析
                </el-button>
              </template>
            </el-table-column>
            <template v-slot:empty>
              <el-empty description="暂无数据"></el-empty>
            </template>
          </el-table>
          <el-config-provider :locale="locale">
            <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
              layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
              @current-change="changeCurrentPage">
            </el-pagination>
          </el-config-provider>
        </div>
        <bottomView />
      </div>
    </div>
  </el-main>
</template>
<script>
// import addEditBaseStudentView from "@/components/evaluate/addEditBaseStudent.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";

export default {
  name: "BaseStudentListView",
  data() {
    return {
      type: 0,
      title: "",
      page: 0,
      pageSize: 10,
      total: 0,
      limit: 10,
      list: [],
      gradeIds: [],

      classList: [],
      form: {
        studentNumber: "",
        name: "",
        idCard: "",
        password: "",
        classId: "",
        mobile: "",
        typeName: "",
      },
    };
  },
  components: {
    // addEditBaseStudentView,
    addFileView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    Upload, bottomView,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  mounted() {
    this.getGradeIds();
    this.getBaseStudentList();
  },
  watch: {
    $route() {
      this.getBaseStudentList();
    },
  },
  methods: {
    filterGradeId(val) {
      //获取班级名称
      httpService("/javaApi/baseClassInfo/getClassInfoByGradeId/" + val, {}, "get").then(
        (data) => {
          console.log(data)
          this.classList = data

        }
      );
    },
    getGradeIds() {
      let date = new Date();
      let nowYear = parseInt(date.getFullYear());
      let nowMonth = parseInt(date.getMonth());
      let gradeIds = [];
      if (nowMonth > 6) {
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
        gradeIds.push(nowYear);
      } else {
        gradeIds.push(nowYear - 3);
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
      }
      this.gradeIds = gradeIds;
    },
    searchEnter: function (e) {
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.getBaseStudentList();
      }
    },
    add() {
      // this.$refs.addEditBaseStudentView.title = "添加学生"
      // this.$refs.addEditBaseStudentView.getBaseClassinfoList();
      // this.$refs.addEditBaseStudentView.dialogFormVisible = true;
    },
    editBaseStudent(obj) {
      let routeData = this.$router.resolve({
        name: "EvReviewByStudentView",
        query: {
          p: obj.studentNumber,
          q: obj.name,
        },
      });
      window.open(routeData.href, "_blank");
    },
    deleteBaseStudent(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService(
            "/javaApi/baseStudent/deleteById",
            "id=" + id,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success("删除成功");
              this.getBaseStudentList();
            } else {
              ElMessage.error("删除失败，请重试！");
            }
          });
        })
        .catch(() => { });
    },
    reloadData() {
      this.page = 1;
      this.getBaseStudentList();
    },
    importExcel() {
      this.$refs.addFileView.dialogFormVisible = true;
      this.$refs.addFileView.title = "批量导入学生";
      this.$refs.addFileView.templateUrl =
        "http://bit.ideamerry.com/%E5%AD%A6%E7%94%9F%E4%BF%A1%E6%81%AF%E5%AF%BC%E5%85%A5%E6%A8%A1%E7%89%88.xls";
      this.$refs.addFileView.uploadApiUrl = "/javaApi/baseStudent/importExcel";
    },
    getBaseClassinfoList() {
      httpService("/javaApi/baseClassInfo/queryAll", {}, "get").then((data) => {
        this.classList = data;
      });
    },
    exportExcel() {
      httpService(
        "/javaApi/baseStudent/exportExcel",
        {
          studentNumber: this.form.studentNumber,
          name: this.form.name,
          classId: this.form.classId,
          gradeId: this.form.gradeId,
        },
        "get"
      ).then((data) => {
        if (data.code == 200) {
          window.open(data.msg, "_blank");
        }
      });
    },
    onSubmit() {
      this.getBaseStudentList();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;
      this.getBaseStudentList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getBaseStudentList();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    getBaseStudentList() {
      httpService(
        "/javaApi/baseStudent/list/" + this.page + "/" + this.pageSize,
        {
          studentNumber: this.form.studentNumber,
          name: this.form.name,
          classId: this.form.classId,
        },
        "get"
      ).then((data) => {
        this.list = data.list;
        this.total = data.total;
        this.pageCount = data.pages;
      });
    },
  },
};
</script>
<style scoped>
.app-body {
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 243px);
}

.el-main {
  padding: 0px;
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 10px;
}

.app-search {
  margin-top: 20px;
}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}
</style>
